/**
 * 
 */
package org.swing.utility.common.datetime;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author lqnhu
 *
 */
public final class TimeUtils {
	private TimeUtils() {
	}

	/**
	 * Prints the duration in a human readable format as X days Y hours Z
	 * minutes etc.
	 *
	 * @param uptime
	 *            the uptime in millis
	 * @return the time used for displaying on screen or in logs
	 */
	public static String printDuration(double uptime) {
		// Code taken from Karaf
		// https://svn.apache.org/repos/asf/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/InfoAction.java
		NumberFormat fmtI = new DecimalFormat("###,###",
				new DecimalFormatSymbols(Locale.ENGLISH));
		NumberFormat fmtD = new DecimalFormat("###,##0.000",
				new DecimalFormatSymbols(Locale.ENGLISH));
		uptime /= 1000;
		if (uptime < 60) {
			return fmtD.format(uptime) + " seconds";
		}
		uptime /= 60;
		if (uptime < 60) {
			long minutes = (long) uptime;
			String s = fmtI.format(minutes)
					+ (minutes > 1 ? " minutes" : " minute");
			return s;
		}
		uptime /= 60;
		if (uptime < 24) {
			long hours = (long) uptime;
			long minutes = (long) ((uptime - hours) * 60);
			String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
			if (minutes != 0) {
				s += " " + fmtI.format(minutes)
						+ (minutes > 1 ? " minutes" : " minute");
			}
			return s;
		}
		uptime /= 24;
		long days = (long) uptime;
		long hours = (long) ((uptime - days) * 24);
		String s = fmtI.format(days) + (days > 1 ? " days" : " day");
		if (hours != 0) {
			s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
		}
		return s;
	}
}
